package classes
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.SampleDataEvent;
	import flash.events.StatusEvent;
	import flash.events.TimerEvent;
	import flash.media.Microphone;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.utils.ByteArray;
	import flash.utils.Timer;
	
	public class RecordManager extends EventDispatcher
	{
		private var _mic:Microphone;
		private var _soundBytes:ByteArray = new ByteArray();
		private var _soundO:ByteArray;
		private var _startTimer:Timer;
		private var _sound:Sound;
		private var _channel:SoundChannel;
		public var recording:Boolean = false;
		public var count:int = 0;
		public function get src():ByteArray{return _soundBytes;}
		public function RecordManager(){
			_startTimer = new Timer( 1000, 3 );
			_startTimer.addEventListener(TimerEvent.TIMER, startTimerHandler );
			_startTimer.addEventListener(TimerEvent.TIMER_COMPLETE, startTimerCompleteHandler );
			_sound = new Sound();
		}
		private function micStatusHandler( event:StatusEvent ):void{
			if( !_mic.muted ){
				count = 0;
				_startTimer.stop();
				_startTimer.reset();
				_startTimer.start();
				startTimerHandler( null );
			}
		}
		private function startTimerHandler( event:TimerEvent ):void{
			count++;
			dispatchEvent( new Event( "count" ));
		}
		private function startTimerCompleteHandler( event:TimerEvent ):void{
			dispatchEvent( new Event( "start" ));
			_mic.setSilenceLevel(0);
			_mic.gain = 80;
			_mic.rate = 44;
			_soundBytes = new ByteArray();	
		}
		private function recordingHandler( event:SampleDataEvent ):void{
			while (event.data.bytesAvailable){
				var sample:Number = event.data.readFloat();
				_soundBytes.writeFloat(sample);
			}
		}
		private var _tempData:Number = 0;
		private function playbackSampleHandler( event:SampleDataEvent ):void{
			dispatchEvent( new RecodeEvent( RecodeEvent.PLAYING, _tempData, _soundO.length ));
			for (var i:int = 0; i < 8192; i++){
				if (_soundO.bytesAvailable < 4) break;
				var sample:Number = _soundO.readFloat();
				event.data.writeFloat(sample);
				event.data.writeFloat(sample);
				_tempData += 4
			}			
		}
		private function soundCompleteHandler( event:Event ):void{
			
		}
		public function start():void{
			recording = true;
			_mic = Microphone.getMicrophone();
			_mic.addEventListener( SampleDataEvent.SAMPLE_DATA, recordingHandler );
			_mic.addEventListener( StatusEvent.STATUS, micStatusHandler );
			if( !_mic.muted ){
				count = 0;
				_startTimer.stop();
				_startTimer.reset();
				_startTimer.start();
				startTimerHandler( null );
			}
		}
		public function end():void{ 
			dispatchEvent( new Event( "end" ));
			recording = false;
			_mic.removeEventListener(SampleDataEvent.SAMPLE_DATA, recordingHandler);
			_soundBytes.position = 0;
			_soundO = new ByteArray();
			_soundO.length=0
			_soundO.writeBytes(_soundBytes);
			_soundO.position = 0;
			_soundBytes.length=0;    
			_mic = null;
		}
		public function play():void{
			_tempData = 0
			if( !_soundO ) return;
			_soundO.position = 0;
			if( !_sound.hasEventListener( SampleDataEvent.SAMPLE_DATA ) ) _sound.addEventListener( SampleDataEvent.SAMPLE_DATA, playbackSampleHandler );	
			_channel = _sound.play();
			if( !_channel.hasEventListener( Event.SOUND_COMPLETE ) ) _channel.addEventListener(Event.SOUND_COMPLETE,soundCompleteHandler )
		}
		public function stop():void{
			
		}
		
	}
}